package com.download.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.download.web.entity.*;
import com.download.web.entity.model.ArticleListModel;
import com.download.web.entity.model.ArticleModel;
import com.download.web.entity.model.Page;
import com.download.web.mapper.BusArticleMapper;
import com.download.web.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.StringJoiner;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cmy
 * @since 2019-02-20
 */
@Service
public class BusArticleServiceImpl extends ServiceImpl<BusArticleMapper, BusArticle> implements IBusArticleService {

    @Resource
    private BusArticleMapper busArticleMapper;
    @Resource
    private IBusArticleLableService articleLableService;
    @Autowired
    private IBusArticleLableService articalLableService;
    @Autowired
    private IBusArticleDownloadService articleDownloadService;
    @Autowired
    private ISysNavBarService sysNavBarService;

    @Autowired
    private IBusImagesService imagesService;
    @Override
    public Page<ArticleListModel> articlePage(Integer pageIndex, Integer pageSize,Integer isRotation) {
        List<ArticleListModel> articleList= busArticleMapper.articlePageByIsRotation(pageIndex*pageSize,pageSize,isRotation);
        Page<ArticleListModel> page=new Page<>(articleList,busArticleMapper.articleCountByIsRotation(isRotation),pageIndex,pageSize);
        return page;
    }

    @Override
    public Page<ArticleListModel> articlePageByTags(Integer pageIndex, Integer pageSize, Integer category, String tags) {
        List<ArticleListModel> articleList= busArticleMapper.articlePageByTags(pageIndex*pageSize,pageSize,category,tags);
        Page<ArticleListModel> page=new Page<>(articleList,busArticleMapper.articleByTagsCount(category,tags),pageIndex,pageSize);
        return page;
    }

    @Override
    public Page<ArticleListModel> articlePageByCategory(Integer pageIndex, Integer pageSize,Integer category) {
        List<ArticleListModel> articleList= busArticleMapper.articlePageByCategory(pageIndex*pageSize,pageSize,category);
        Page<ArticleListModel> page=new Page<>(articleList,busArticleMapper.articleCount(),pageIndex,pageSize);
        return page;
    }


    @Override
    public void saveArticle(ArticleModel articleModel) {
        SysNavBar sysNavBar=sysNavBarService.getOne(new QueryWrapper<SysNavBar>().eq("id",articleModel.getUnderType()));
        //保存文章
        BusArticle  busArticle=new BusArticle(articleModel.getId(),articleModel.getContent(),articleModel.getTitle(),
                sysNavBar.getParentId(),articleModel.getUnderType(),articleModel.getVisibleType(),articleModel.getMetaKeywords(),
                articleModel.getMetaTitle(),articleModel.getIsRotation(),new Date(),0,articleModel.getCategory(),articleModel.getAppName(),articleModel.getMetaDescribtion());
        busArticleMapper.insert(busArticle);
//        busArticleMapper.insertArticle(articleModel);
        //保存文章标签
        List<BusArticleLable> articleLables=articleModel.getLabelVals().stream().map(
                l-> new BusArticleLable(l,busArticle.getId())
        ).collect(Collectors.toList());
        articalLableService.saveBatch(articleLables);
        imagesService.save(new BusImages(null,articleModel.getAppImg(),"1",busArticle.getId()));
        //保存文章对应软件的下载地址
        /*List<BusArticleDownload> downloads=articleModel.getDownloadUrl();
        downloads.forEach(d->{
            d.setArticleId(articleModel.getId());
        });
        articleDownloadService.saveBatch(downloads);*/
    }

    @Override
    public void updateArticle(ArticleModel articleModel) {
        SysNavBar sysNavBar=sysNavBarService.getOne(new QueryWrapper<SysNavBar>().eq("id",articleModel.getUnderType()));
        //保存文章
        BusArticle  busArticle=new BusArticle(articleModel.getId(),articleModel.getContent(),articleModel.getTitle(),
                sysNavBar.getParentId(),articleModel.getUnderType(),articleModel.getVisibleType(),articleModel.getMetaKeywords(),
                articleModel.getMetaTitle(),articleModel.getIsRotation(),articleModel.getCategory(),articleModel.getAppName(),articleModel.getMetaDescribtion());
        busArticleMapper.updateById(busArticle);
        //保存文章标签
        articalLableService.remove(new QueryWrapper<BusArticleLable>().eq("article_id",articleModel.getId()));
        List<BusArticleLable> articleLables=articleModel.getLabelVals().stream().map(
                l-> new BusArticleLable(l,articleModel.getId())
        ).collect(Collectors.toList());
        articalLableService.saveBatch(articleLables);
        //保存文章对应软件的下载地址
//        articleDownloadService.remove(new QueryWrapper<BusArticleDownload>().eq("article_id",articleModel.getId()));

        //文章图片
        BusImages images=imagesService.getOne(new QueryWrapper<BusImages>().eq("article_id",articleModel.getId()));
        if (images==null){
            images=new BusImages();
            images.setIsThumbnail("1");
            images.setArticleId(busArticle.getId());
        }
        images.setPath(articleModel.getAppImg());
        imagesService.saveOrUpdate(images);
        /*List<BusArticleDownload> downloads=articleModel.getDownloadUrl();
        downloads.forEach(d->{
            d.setArticleId(articleModel.getId());
        });
        articleDownloadService.saveBatch(downloads);*/
    }

    @Override
    public ArticleModel findArticleById(Integer id) {
        ArticleModel articleModel=busArticleMapper.findArticleInfoById(id);
        articleModel.setDownloadUrl(articleDownloadService.list(new QueryWrapper<BusArticleDownload>().eq("article_id",id)));
        articleModel.setLabelVals(articleLableService.articalLables(id));
        return articleModel;
    }

    @Override
    public void addBrowseNum(Integer id) {
        busArticleMapper.browseNumAdd(id);
    }

    @Override
    public Page<ArticleListModel> articlePageByType(Integer pageIndex, Integer pageSize, Integer type) {
        List<ArticleListModel> articleList= busArticleMapper.articlePageByTypeId((pageIndex-1)*pageSize,pageSize,type);
        articleList.forEach(a->{
            List<String> lables=  articleLableService.articalLables(a.getId());
            a.setTags(StringUtils.join(lables, ","));
        });
        Page<ArticleListModel> page=new Page<>(articleList,busArticleMapper.articleCountByType(type),pageIndex,pageSize);
        return page;
    }

    @Override
    public Page<ArticleListModel> articlePageByTypeAndKeywords(String keywords, Integer pageIndex, Integer pageSize, Integer type) {
        List<ArticleListModel> articleList= busArticleMapper.articlePageByTypeAndKeywords((pageIndex-1)*pageSize,pageSize,
                type,keywords);
        articleList.forEach(a->{
            List<String> lables=  articleLableService.articalLables(a.getId());
            a.setTags(StringUtils.join(lables, ","));
        });
        Page<ArticleListModel> page=new Page<>(articleList,busArticleMapper.articleCountByType(type),pageIndex,pageSize);
        return page;
    }

    @Override
    public Page<ArticleListModel> articlePageByKeyword(Integer pageIndex, Integer pageSize, String keyword) {
        List<ArticleListModel> articleList=busArticleMapper.articlePageByKeyword(pageIndex,pageSize,keyword);
        Page<ArticleListModel> page=new Page<>(articleList,busArticleMapper.articleCountByKeyword(keyword),pageIndex,pageSize);
        return page;
    }

    @Override
    public BusArticle articleByLatestTag(String tags) {
        return busArticleMapper.oneArtByTags(tags);
    }
}
